package com.itcat.api;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itcat.api.mysql.PmsProductAttributeApi;
import com.itcat.mapper.PmsProductAttributeMapper;
import com.itcat.mapper.PmsProductCategoryAttributeRelationMapper;
import com.itcat.mysql.param.PageParam;
import com.itcat.mysql.pojo.PmsProduct;
import com.itcat.mysql.pojo.PmsProductAttribute;
import com.itcat.mysql.pojo.PmsProductCategoryAttributeRelation;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Jerry Broker
 * @project_name rainbow-mall
 * @create 2022-04-03 0:56
 */
@DubboService
public class PmsProductAttributeApiImpl implements PmsProductAttributeApi {

    @Autowired
    private PmsProductAttributeMapper pmsProductAttributeMapper;
    @Autowired
    private PmsProductCategoryAttributeRelationMapper mapper;

    /**
     * 通过ProductAttributeCategoryId查找PmsProductAttribute
     *
     * @param id
     * @return
     */
    @Override
    public List<PmsProductAttribute> findByCategoryId(Long id) {
        LambdaQueryWrapper<PmsProductAttribute> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(PmsProductAttribute::getProductAttributeCategoryId, id);
        List<PmsProductAttribute> selectList = pmsProductAttributeMapper.selectList(queryWrapper);
        return selectList;
    }

    /**
     * 根据分类查询属性列表或参数列表
     *
     * @param cid
     * @param pageParam
     * @param type
     * @return
     */
    @Override
    public Page<PmsProductAttribute> list(Integer cid, PageParam pageParam, Integer type) {
        Page<PmsProductAttribute> page = new Page<>(pageParam.getPageNum(), pageParam.getPageSize());
        LambdaQueryWrapper<PmsProductAttribute> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(PmsProductAttribute::getProductAttributeCategoryId, cid);
        queryWrapper.eq(PmsProductAttribute::getType, type);
        Page<PmsProductAttribute> selectPage = pmsProductAttributeMapper.selectPage(page, queryWrapper);
        return selectPage;
    }

    @Override
    public List attrInfo(Integer productCategoryId) {
        List<Map<String, Object>> list = new ArrayList<>();

        LambdaQueryWrapper<PmsProductCategoryAttributeRelation> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(PmsProductCategoryAttributeRelation::getProductCategoryId, productCategoryId);
        List<PmsProductCategoryAttributeRelation> pmsProductAttributes = mapper.selectList(queryWrapper);
        for (PmsProductCategoryAttributeRelation pmsProductAttribute : pmsProductAttributes) {
            Map<String, Object> map = new HashMap();
            map.put("attributeId", pmsProductAttribute.getProductAttributeId());
            PmsProductAttribute pmsProductAttribute1 = pmsProductAttributeMapper.selectById(pmsProductAttribute.getProductAttributeId());
            map.put("attributeCategoryId", pmsProductAttribute1.getProductAttributeCategoryId());
            list.add(map);

        }

        return list;
    }
}
